package com.yhf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yhf.bean.Admin;
import com.yhf.service.AdminService;
import com.yhf.utils.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {
    private Map<String,Object> result = new HashMap<>();
    @Autowired
    private AdminService adminService;
    @RequestMapping("/goAdminListView")
   public String goAdminListView(){
       return "admin/adminList";
   }
@PostMapping("/getAdminList")
@ResponseBody
   public Map<String,Object> getAdminList(Integer page,Integer rows,String username){
    Admin admin = new Admin();
    admin.setName(username);
    //设置每页记录数
    PageHelper.startPage(page,rows);
    //获取指定的和管理员或者是所有的管理员数据
    List<Admin> adminList = adminService.getAdminList(admin);
    PageInfo<Admin> adminPageInfo = new PageInfo<>(adminList);
    //总页数
    long total = adminPageInfo.getTotal();
    result.put("total",total);
    result.put("rows",adminList);
    return result;
}
@PostMapping("/addAdmin")
@ResponseBody
public Map<String,Object> addAdmin(Admin admin){
       Admin admin1 = adminService.findByName(admin.getName());
    if (admin1==null){
        if (adminService.addAdmin(admin)>0){
            result.put("success",true);
        }else {
            result.put("success",false);
            result.put("msg","服务器发生异常，添加失败！");
        }

    }else {
        result.put("success",false);
        result.put("msg","该用户名已存在，请更改重试！");
    }
    return result;
}
@PostMapping("/editAdmin")
@ResponseBody
public Map<String, Object> editAdmin(Admin admin) {
         //需要排除用户名只修改账户名以外信息
        Admin admin1 = adminService.findByName(admin.getName());
         if (admin1 != null) {
             if (!(admin.getId().equals(admin1.getId()))) {
                 result.put("success", false);
                 result.put("msg", "该用户名已存在，请更改重试！");
                 return result;
                 }


             }

         //修改操作
         if (adminService.updateAdmin(admin) > 0) {
             result.put("success", true);
             } else {
             result.put("success", false);
             result.put("msg", "服务器异常，修改失败！");
             }
        return result;

         }
    @PostMapping("/deleteAdmin")
 @ResponseBody
 public Map<String, Object> deleteAdmin(@RequestParam(value = "ids[]", required = true) Integer[] ids) {
         if (adminService.deleteAdmin(ids) > 0) {
             result.put("success", true);
             } else {
             result.put("success", false);
             result.put("msg", "服务器异常，删除失败！");
             }
         return result;
         }
@PostMapping("/uploadPhoto")
@ResponseBody
         public Map<String,Object> uploadPhoto(MultipartFile photo, HttpServletRequest request){
     final  String realPath = request.getServletContext().getRealPath("/uplod/admin_portrait/");
    final String portraitPath = request.getServletContext().getContextPath() + "/uplod/admin_portrait/";
    return UploadFile.getUploadResult(photo,realPath,portraitPath);
}

}
